<?
    class AccountsController extends Principio_Library_ControllerAction
    {
        public function init()
        {
            parent::init();
            $this->view->menu = array(
                    "Edit" => "edit",
                    "Delete" => "delete",
                );
        }



        public function indexAction()
        {
            if ($this->is_post)
                $this->view->title = "Accounts";
        }


        private function validate($is_add = false)
        {
            if ($this->checkPostItemString("login", "Login"))
                $this->view->model_account->setLogin($this->post_data['login']);

            if ($this->checkPostItemString("password", "Password"))
                $this->view->model_account->setPassword($this->post_data['password']);

            if ($this->checkPostItemString("display_name", "Display name"))
                $this->view->model_account->setDisplayName($this->post_data['display_name']);

            if ($this->checkPostItemString("short_name", "Short name"))
                $this->view->model_account->setShortName($this->post_data['short_name']);

            if (!$is_add)
            {
                if ($this->checkPostItemInt("company_id", "Company ID"))
                    $this->view->model_account->setCompanyID($this->post_data['company_id']);
            };

            if (!Principio_Library_Common::$is_error)
                Principio_Library_Common::checkValidity(Principio_Model_AccountMapper::checkUniqueLogin($this->view->model_account), "Account with login '".$this->view->model_account->getLogin()."' already exists in this company");

            return !Principio_Library_Common::$is_error;
        }


        public function editAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("id", "Account ID"))
                {
                    $this->view->action = "edit";
                    $this->view->model_account = Principio_Model_AccountMapper::get($this->post_data['id']);
                    $model_account_old = Principio_Model_AccountMapper::get($this->post_data['id']);

                    if ($this->is_apply)
                    {
                        if ($this->validate())
                        {
                            if (!Principio_Library_Common::$is_error)
                            {
                                Principio_Model_AccountMapper::save($this->view->model_account);
                                Principio_Library_Common::$msg = "Account was updated.";
                                $this->view->model_account = Principio_Model_AccountMapper::get($this->post_data['id']);
                            };
                        };
                    };

                    $this->view->title = "View and Edit account [".(Principio_Library_Common::$is_error ? $model_account_old->getLogin() : $this->view->model_account->getLogin())."]";
                    $this->view->companies = Principio_Model_CompanyMapper::getByType(Principio_Model_CompanyMapper::TYPE_COMPANY);
                };
            };
        }


        public function addAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("company_id", "Company ID")))
            {
                $this->view->title = "Add new account";
                $this->view->is_add = true;
                $this->view->action= "add";
                $this->view->model_account = new Principio_Model_Account();
                $this->view->model_account->setCompanyID($this->post_data['company_id']);

                if ($this->is_apply)
                {
                    if ($this->validate())
                    {
                        if (($account_id = Principio_Model_AccountMapper::save($this->view->model_account)) !== false)
                        {
                            $this->view->model_account->setID($account_id);
                            $this->view->action = "edit";
                            $this->view->is_add = false;
                            Principio_Library_Common::$msg = "Account was added.";
                            $this->view->title = "View and Edit account '".$this->view->model_account->getLogin()."'";
                        };
                    };
                };

                $this->view->companies = Principio_Model_CompanyMapper::getByType(Principio_Model_CompanyMapper::TYPE_COMPANY);
            };
        }


        public function deleteAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("id", "Account UD"))
                {
                    if (Principio_Model_AccountMapper::delete($this->post_data['id']))
                        Principio_Library_Common::$msg = "Account was deleted.";
                };
            };
        }


        public function getlistAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("company_id", "Company ID"))
                    $this->view->accounts = Principio_Model_AccountMapper::getInCompany($this->post_data['company_id']);
            };
        }
}